<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let input = `3
        5000
        4000
        3000`
        let list = input.split('\n').map((v) => parseInt(v))
        let N = list.shift()
        console.log(N, list);

        class treeNode {
            constructor() {
                this.value = null
                this.left = null
                this.mid = null
                this.right = null
            }
        }
        let node = new treeNode()
        let maxh = 0

        function biuldTree(node, value, h) {


            if (node.value === null) {
                node.value = value
                maxh = 1
            } else {
                let newnode = new treeNode()
                newnode.value = value
                if (h > maxh) {
                    maxh = h + 1
                }
                if (value < node.value - 500) {
                    //左子树
                    if (node.left === null) {
                        node.left = newnode

                        return
                    } else {
                        biuldTree(node.left, value, h + 1)
                    }

                } else if (value > node.value + 500) {
                    //右子树
                    if (node.right === null) {
                        node.right = newnode
                        return
                    } else {
                        biuldTree(node.right, value, h + 1)
                    }
                } else {
                    // 中子树
                    if (node.mid === null) {
                        node.mid = newnode
                        return
                    } else {
                        biuldTree(node.mid, value, h + 1)
                    }
                }
            }
        }

        for (let i = 0; i < list.length; i++) {
            biuldTree(node, list[i], maxh)
        }
        console.log(node);
        console.log(maxh);











    </script>
</body>

</html>